package JDBC.dao;

import JDBC.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class UserDAOImpl implements UserDAO{

    @Override
    public void addUser() {
        User user = new User();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        user.setUsername(sc.next());
        System.out.println("请输入密码：");
        user.setPassword(sc.next());
        System.out.println("请输入昵称：");
        user.setNickname(sc.next());
        System.out.println("请输入年龄：");
        user.setAge(sc.nextInt());
        try(Connection connection = DBUtil.getConnection()) {
            String sql = "INSERT INTO user (username,password,nickname,age) VALUES(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getNickname());
            ps.setInt(4,user.getAge());
            int num = ps.executeUpdate();
            if (num > 0){
                System.out.println("添加成功");
            }else{
                System.out.println("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public User login() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        User user = new User();
        try(Connection connection = DBUtil.getConnection()) {
            String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                System.out.println("登录成功");
                user = getUser(rs);
            }else {
                System.out.println("登录失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public void updatePassword() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.next();
        System.out.println("请输入新密码：");
        String password = sc.next();
        try(Connection connection = DBUtil.getConnection()) {
            String sql = "UPDATE user SET password = ? WHERE username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,password);
            ps.setString(2,username);

            int num = ps.executeUpdate();
            if (num > 0){
                System.out.println("更新成功");
            }else{
                System.out.println("更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delect() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的用户名");
        String username = sc.next();
        try(Connection connection = DBUtil.getConnection()) {
            String sql = "DELETE FROM user WHERE username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            int num = ps.executeUpdate();
            if (num > 0){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public User findUserById() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查询的id");
        int id = sc.nextInt();
        User user = new User();
        try(Connection connection = DBUtil.getConnection()) {
            String sql = "SELECT * FROM user WHERE id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1,id);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                System.out.println("查找成功");
                user = getUser( rs);
            }else {
                System.out.println("查找失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(user);
        return user;
    }

    @Override
    public User findUserByusername() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String  username = sc.next();
        User user = new User();
        try(Connection connection = DBUtil.getConnection()) {
            String sql = "SELECT * FROM user WHERE username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                System.out.println("查找成功");
                user = getUser( rs);
            }else {
                System.out.println("查找失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(user);
        return user;
    }

    @Override
    public List<User> findAll() {
        List<User> list = new ArrayList<>();
        User user = new User();
        try(Connection connection = DBUtil.getConnection();) {
            String sql = "SELECT * FROM user";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                user = getUser(rs);
                list.add(user);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        list.forEach(System.out::println);
        return list;
    }
    private User getUser(ResultSet rs) throws SQLException {
        User user = new User();
        user.setId(rs.getInt("id"));
        user.setUsername(rs.getString("username"));
        user.setPassword(rs.getString("password"));
        user.setNickname(rs.getString("nickname"));
        user.setAge(rs.getInt("age"));
        return user;
    }
}
